//
//  MYXLuckyTxteViewCell.m
//  meiyinxiang
//
//  Created by apple on 2017/11/1.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXLuckyTxteViewCell.h"
#import <NIMGrowingInternalTextView.h>
#import "MYXPhotoItem.h"
#import "LuckyMoneyViewController.h"

@interface MYXLuckyTxteViewCell(){
    MYXPhotoItem *oneItem;
    BOOL isFirstVersion; //第一版本
}

@property (nonatomic,strong) NSMutableArray *imgViewArray;
@property (nonatomic,strong) NSMutableArray *urls;
@end

@implementation MYXLuckyTxteViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self.growTextView];
    if (self.uploadImgArray == nil) {
      self.uploadImgArray = [NSMutableArray array];
    }
    isFirstVersion = NO;
    [self addImgsView];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+(MYXLuckyTxteViewCell *)loadNibWithTxteViewCell{
    UINib *nib = [UINib nibWithNibName:@"MYXLuckyTxteViewCell" bundle:[NSBundle mainBundle]];
    return [[nib instantiateWithOwner:nil options:nil] lastObject];
    

}
#pragma mark - PrivateMethod
- (void)textChange{
    LuckyMoneyViewController  *msuperVC = (LuckyMoneyViewController  *)self.mSuperViewController;
    if (self.growTextView.text.length >= 2) {
        [msuperVC.sendLuckyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        [msuperVC.sendLuckyBtn setTitleColor:[UIColor lightTextColor] forState:UIControlStateNormal];
    }
    self.placeholderLabel.hidden = self.growTextView.hasText;
}
- (void)addImgsView{//提前创建imgviews
        for (NSInteger i = 0; i < 9; i++) {
            NSInteger line = i/3;//行
            NSInteger row = (i - line*3)%3;//列
            MYXPhotoItem *item = [MYXPhotoItem loadNibWithPhotoItem];
            
            item.frame = CGRectMake((PostLukyItemSpace + PostLukyItemWith)*row + PostLukylineSpace,PostLukyOriginalY + PostLukylineSpace + (PostLukyItemWith+PostLukyItemSpace)*line, PostLukyItemWith, PostLukyItemWith);
// [item.deleteBtn addTarget:self action:@selector(deleteAct:) forControlEvents:UIControlEventTouchUpInside];
            WS(weakSelf);
            item.tapDeleteBlock = ^(NSString * tagstring){
                NSInteger tag = tagstring.integerValue - 100;
                [weakSelf.imgArray removeObjectAtIndex:tag];
                //[weakSelf.uploadImgArray removeObjectAtIndex:tag];
                [weakSelf postImgsNotification:weakSelf.imgArray];
            };
            item.imageView.contentMode = UIViewContentModeScaleAspectFill;
            item.imageView.layer.masksToBounds = YES;
            item.deleteBtn.tag = 10 + i;
            item.tag = i + 100;
            item.hidden = YES;
            [self addImgTapGesWithImgView:item.imageView];
            [self.imgViewArray addObject:item.imageView];
            [self addSubview:item];
        }
   
}
//- (void)recieveNoti:(NSNotification *)noti{
//    NSMutableArray * imgArray = (NSMutableArray *)noti.object;
//    self.imgArray = imgArray;
//    [self addImgToImageView];
//
//}

- (void)deleteOneItem{
    oneItem.hidden = YES;
    [self.imgArray removeAllObjects];
   // [self.uploadImgArray removeAllObjects];
    [self postImgsNotification:self.imgArray];
}
- (void)postImgsNotification:(NSArray*)data{
    [[NSNotificationCenter defaultCenter] postNotificationName:PostLukyImgNotificationName object:data];
}
- (void)addImgToImageView{
    if (self.imgArray.count == 2 && isFirstVersion == YES) {
        [self.imgArray removeObjectAtIndex:0];
    }
    WS(weakSelf);
        if (self.imgArray.count == 1 ) {
            for (NSInteger i = 0; i<2; i++) {//剩下一张时，显示两张bug解决
                NSInteger tag = 100 + i;
                MYXPhotoItem *photoItem = [self viewWithTag:tag];
                photoItem.hidden = YES;
            }
            UIImage *image = self.imgArray[0];
            CGFloat heightCons = 110;
            CGFloat withCons = 175;
            if (oneItem == nil) {
                oneItem = [MYXPhotoItem loadNibWithPhotoItem];
            }
            CGFloat imageHeight = image.size.height;
            CGFloat imageWidth = image.size.width;
            if (imageWidth <  imageHeight) {
                heightCons = withCons/(imageWidth/imageHeight);
                
            }else{
                withCons = ScreenWidth - 20;
                heightCons = withCons/(imageWidth/imageHeight);
                
            }
            CGFloat x = (ScreenWidth - withCons)/2;
            CGFloat y = PostLukyOriginalY + PostLukylineSpace;
            oneItem.frame = CGRectMake(x, y, withCons, heightCons);
//            [oneItem.deleteBtn addTarget:self action:@selector(deleteOneItem) forControlEvents:UIControlEventTouchUpInside];
            oneItem.tapDeleteBlock = ^(NSString *deleteString){
                [weakSelf deleteOneItem];
            };
            oneItem.imageView.image = image;
            oneItem.hidden = NO;
//            __weak typeof(self) weakself = self;
//            [oneItem uploadImgWithFinishBlock:^(NSString * imgurl) {
//                if (weakself.uploadImgArray.count > 0) {
//                    [weakself.uploadImgArray removeAllObjects];
//                    [weakself.uploadImgArray addObject:imgurl];
//                }else{
//                    [weakself.uploadImgArray addObject:imgurl];
//                }
//            }];
            [self addSubview:oneItem];
        }else{
            if (self.imgArray.count == 0) {
                oneItem.hidden = YES;
            }
            for (NSInteger i = 0; i<9; i++) {
                NSInteger tag = 100 + i;
                MYXPhotoItem *photoItem = [self viewWithTag:tag];
                if (i < self.imgArray.count) {
                    photoItem.imageView.image =  self.imgArray[i];
                    photoItem.hidden = NO;
                }else{
                    photoItem.imageView.image = nil;
                    photoItem.hidden = YES;
                }
//                [photoItem uploadImgWithFinishBlock:^(NSString *imgurl) {
//                    [weakSelf.uploadImgArray addObject:imgurl];
//                }];
            }
        }
    
}
- (void)uploadImgs{
    if (self.uploadImgArray.count > 0) {
        [self.uploadImgArray removeAllObjects];
    }
    if (self.imgArray.count == 1) {
    __weak typeof(self) weakself = self;
  [oneItem uploadImgWithFinishBlock:^(NSString * imgurl) {
    [weakself.uploadImgArray addObject:imgurl];
    
}];
        return;
    }else{
        for (NSInteger i = 0; i<9; i++) {
            NSInteger tag = 100 + i;
            MYXPhotoItem *photoItem = [self viewWithTag:tag];
            if (!photoItem.hidden) {
                [photoItem uploadImgWithFinishBlock:^(NSString *imgurl) {
                    [self.uploadImgArray addObject:imgurl];
                }];
            }
            
        }
    }
    
    
}
- (void)addImgTapGesWithImgView:(UIImageView *)imgView{
    //点击查看照片
    UITapGestureRecognizer *tapImg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgAct:)];
    [imgView addGestureRecognizer:tapImg];
    imgView.userInteractionEnabled = YES;
}
- (void)tapImgAct:(UITapGestureRecognizer *)ges{
    MYXPhotoItem * photoItem = (MYXPhotoItem *)ges.view.superview;
    NSInteger tag = photoItem.tag - 100;
    [self.urls removeAllObjects];
    for (UIImage *subImg in self.imgArray) {
        [_urls addObject:@""];
    }
    HMPhotoBrowserController *hmPhotoVC = [HMPhotoBrowserController photoBrowserWithSelectedIndex:tag urls:_urls parentImageViews:self.imgViewArray];
    [self.mSuperViewController presentViewController:hmPhotoVC animated:YES completion:nil];
}
- (NSMutableArray *)imgViewArray{
    if (_imgViewArray == nil) {
        _imgViewArray = [NSMutableArray array];
    }
    return _imgViewArray;
}
- (NSMutableArray *)urls{
    if (_urls == nil) {
        _urls = [NSMutableArray array];
    }
    return _urls;
}
- (void)removeFromSuperview{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self.growTextView];
}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
